/**
 * 
 */
package com.laozhao.dreamframe.model;

/**
 * @author LaoZhao
 *
 */
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.laozhao.dreamframe.exception.ModelMapException;
import com.laozhao.dreamframe.sqlmap.BaseSelector;

public class ModelUtil {
//	private transient Log log = LogFactory.getLog(this.getClass());
	
	/**
	 * 通过传递进来的ResultSet与对象名称,序列化为对象,塞入list
	 * 
	 * @param rs
	 *            结果集
	 * @param objName
	 *            对象
	 * @return list list里存储传递的objName的对象
	 */
	public static <E> List<E> Relection(ResultSet rs, Class<E> clazz)     {
		List<E> list = new ArrayList<E>();
		try {
			// 1、得到resultSet 的行
			if (rs == null){ // 判断是否结果集为空,如果为空,直接返回
				return null;
			}
			// 2、创建对象，得到对象拥有@Column标签的方法
//			Class clazz = Class.forName(objName);
			E obj = clazz.newInstance();
			
			Field[] fields = clazz.getDeclaredFields() ;

			while (rs.next()) {
				
				// 3、循环方法列表，通过标签中的[name]得到列名，通过列名得到结果集中的value
				for (int i = 0; i < fields.length; i++) {
					Field field = fields[i];
					DreamDBColumn column = field.getAnnotation(DreamDBColumn.class) ;
					if(column != null ){
						String colName = column.dbColunName();
						if(colName != null && !"".equals(colName)){
							Method method = clazz.getMethod("set"
									+ field.getName().substring(0,1).toUpperCase() 
									+ field.getName().substring(1, field.getName().length()),
									new Class[] { field.getType() });

							// 4.执行SET方法，设置值到object
							if (field.getType().equals(String.class)) {
								// 处理当为String类型情况
								method.invoke(obj, new Object[] { rs.getString(colName) });
							} else if (field.getType().equals(Integer.class)
									|| field.getType().equals(int.class)) {
								// 处理当为Integer类型情况
								method.invoke(obj, new Object[] { rs.getInt(colName) });
							} else if (field.getType().equals(Float.class)
									|| field.getType().equals(float.class)) {
								// 处理当为Float类型情况
								method.invoke(obj, new Object[] {rs.getFloat(colName) });
							} else if (field.getType().equals(Double.class)
									|| field.getType().equals(double.class)) {
								// 处理当为Double类型情况
								method.invoke(obj, new Object[] { rs.getDouble(colName) });
							} else if (field.getType().equals(BigDecimal.class)) {
								// 处理当为BigDecimal类型情况
								method.invoke(obj, new Object[] { rs.getBigDecimal(colName) });
							} else if (field.getType().equals(Long.class)
									|| field.getType().equals(long.class)) {
								// 处理当为Long类型情况
								method.invoke(obj, new Object[] {rs.getLong(colName) });
							} else if (field.getType().equals(Timestamp.class)) {
								// 处理当为Timestamp类型情况
								method.invoke(obj, new Object[] { rs.getTimestamp(colName) });
							
							} else if (field.getType().equals(Date.class)) {
								// 处理当为Date类型情况
								method.invoke(obj, new Object[] { rs.getDate(colName) });
							} else if (field.getType().equals(short.class)) {
								// 处理当为Short类型情况
//								log.debug(" // parse short // ");
								method.invoke(obj, new Object[] { rs.getShort(colName) });
							}
							
						}
					}
				}
				//添加到List
				list.add(obj);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new ModelMapException(e);
		}catch (InstantiationException e) {
			e.printStackTrace();
			throw new ModelMapException(e);
			
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			throw new ModelMapException(e);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			throw new ModelMapException(e);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			throw new ModelMapException(e);
		} catch (SecurityException e) {
			e.printStackTrace();
			throw new ModelMapException(e);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			throw new ModelMapException(e);

//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//			throw new ModelMapException(e);
		}
		return list;
	}

	public static String getSelStr(Class<?> clazz) {
		StringBuffer sqlBuffer = new StringBuffer("Select * from ");
		
		// 2、创建对象，得到对象拥有@DreamDBTable标签的方法
		DreamDBTable table = clazz.getAnnotation(DreamDBTable.class) ;
		String tableName = table.dbTableName();
		sqlBuffer.append(tableName);
		BaseSelector selector = new BaseSelector(sqlBuffer.toString(), null, null);
		
		return selector.toSql();
	}
}